/**
 * Created by lixiaodi on 2018/10/18.
 */
//页面初始化

var ascDescData = [
	{id : "asc", text : "正序"},
	{id : "desc", text : "倒序"}
];
var childReportId = window.location.href;
var arr = childReportId.split("=");
childReportId = arr[1].split("&")[0];

mini.parse();
mini_debugger = false;
//得到页面上元素
var grid = mini.get("datagrid1");
var countGrid = mini.get("countGrid");
var ExcelBtn = mini.get("ExcelBtn");
var sortFiledInput = mini.getByName("sortFileds");	//排序：列名下拉框
var ascDesc = mini.getByName("ascDesc");			//排序：正序倒序下拉框
ascDesc.setData(ascDescData);
//路径
var url = bootPATH + "../frame/frame1/pages/";      //路径前缀

//全局变量（初始化请求的返回值）
var pubReportName;		//报表名字
var pubReportData;		//报表数据
var pubColumnCnType;	//列名数据
var pubSerchData;		//搜索框数据
var pubCountDate;		//综合数据
var pubPageCount;		//数据总数

//分页控件
var pager = grid.getBottomPager();

//声明需要显示得搜索框得样式
var inputList = {
    str         : "<td style='width: 7%;text-align: right;'>{{title}}：</td>" +
                  "<td style='width: 18%;'><input id='{{dataSource}}1' name='{{dataSource}}1' class='mini-textbox' style='width: 82%;'/></td>",
    dateStr     : "<td style='width: 7%;text-align: center;'>{{title}}：</td>" +
                  "<td><input id='{{dataSource}}1' name='{{dataSource}}1' class='mini-datepicker' style='width: 82%;'/></td>",
    codeStr     : "<td style='text-align: right;'>{{title}}：</td>" +
                  "<td>" +
                  "<input  id='{{dataSource}}1' name='{{dataSource}}1' class='mini-textbox' style='width: 40%;'/>" +
                  "<span>-</span>" +
                  "<input  id='{{dataSource}}2' name='{{dataSource}}2' class='mini-textbox' style='width: 40%;'/>" +
                  "</td>",
    dateSection : "<td style='width: 7%;text-align: right;'>{{title}}：</td>" +
                  "<td>" +
                  "<input id='{{dataSource}}1'name='{{dataSource}}1' class='mini-datepicker' style='width: 48%;'/>" +
                  "<span>-</span>" +
                  "<input id='{{dataSource}}2' name='{{dataSource}}2' class='mini-datepicker' style='width: 48%;'/>" +
                  "</td>",
    pullDown    : "<td style='width: 7%;text-align: right;'>{{title}}：</td>" +
                  "<td style='width: 18%;'><input id='{{dataSource}}' name='{{dataSource}}1' class='mini-combobox' showNullItem='true' nullItemText='空' onclick='pullDown(this)' style='width: 82%;'></td>"
}
//存放替换后样式得字符串
var str1;
var inputStrStrArr = [];
//追加样式函数(数据绑定)
function replaceInput(inputStr,reportDatas){
    str1 = inputStr.replace(/{{(\w+)}}/g,function(match,$1){
        return reportDatas[$1];
    });
    inputStrStrArr.push(str1);
}
//页面初始化(数据表/搜索框/总数表)
$(function(){
    //使用ajax的回调函数
    $.when(getAjaxData()).done(function(data){
    	console.log(data,"初始化值");
    	//如果有搜索条件
        $("#fieldsetBox").show();
        //得到ajax返回的数据
        var responseData =  data;
        if(data.pageCount && data.pageCount.length > 0){
        	//总条数
            var pageCount = data.pageCount[0].id;
        }
        //数据表显示======================================================================
        var reportName = responseData.reportParam.reportName;   	//报表名称
        var reportData = responseData.data;                     	//表格列数据/内容数据
        var columnCnType = responseData.columnCnType;           	//列名
        var serchData = responseData.serchData;                 	//搜索框
        var countDate = responseData.countDate;                 	//总数
        var btnIfShow = responseData.buttonIfShowVo;				//按钮显示
        //赋值给全局变量
        pubReportName = reportName;		//报表名字
        //pubReportData = reportData;		//报表数据
        pubColumnCnType = columnCnType;	//列名数据
        pubSerchData = serchData;		//搜索框数据
        //pubCountDate = countDate;		//综合数据
        pubPageCount = pageCount;		//数据总数
        //下拉框数据格式处理
        var sortFiledData = [];
        for(var s = 0; s < columnCnType.length; s++){
        	var sortFiledsObj = {};
        	sortFiledsObj.text = columnCnType[s].column_name.split("$")[2];
        	sortFiledsObj.id = columnCnType[s].column_name.split("$")[0].concat(".",columnCnType[s].column_name.split("$")[1]);
        	sortFiledData.push(sortFiledsObj);
        }
        //排序下拉框赋值
        sortFiledInput.setData(sortFiledData);
        //根据不同的报表名称显示不同的按钮
        if(reportName == "合同查询"){
        	//判断Excle是否显示按钮(0:显示)
        	if(btnIfShow.excelBtn == "0"){
        		//Excel按钮显示
        		$("#Excels")[0].style.display = "inline-block";
        	}else{
        		$("#Excels")[0].style.display = "none";
        	}
        	//判断合同修改是否显示按钮(0:显示)
        	if(btnIfShow.updateBtn == "0"){
        		//合同修改按钮显示
            	$("#editBtn")[0].style.display = "inline-block";
        	}else{
        		$("#editBtn")[0].style.display = "none";
        	}
        	
        }else if(reportName == "支出明细"){
        	//判断Excle是否显示按钮(0:显示)
        	if(btnIfShow.excelBtn == "0"){
        		//Excel按钮显示
        		$("#Excels")[0].style.display = "inline-block";
        	}else{
        		$("#Excels")[0].style.display = "none";
        	}
        	//判断新增凭证是否显示按钮(0:显示)
        	if(btnIfShow.addBtn == "0"){
        		 //新增凭证按钮显示
        		 $("#newVoucherBtn")[0].style.display = "inline-block";
        	}else{
        		//新增凭证按钮隐藏
        		$("#newVoucherBtn")[0].style.display = "none";
        	}
        	//判断导入凭证是否显示按钮(0:显示)
        	if(btnIfShow.importBtn == "0"){
        		 //导入凭证按钮显示
        		$("#importVoucherBtn")[0].style.display = "inline-block";
        	}else{
        		//导入凭证按钮隐藏
        		$("#importVoucherBtn")[0].style.display = "none";
        	}
        }else{
        	//判断Excle是否显示按钮(0:显示)
        	if(btnIfShow.excelBtn == "0"){
        		//Excel按钮显示
        		$("#Excels")[0].style.display = "inline-block";
        	}else{
        		$("#Excels")[0].style.display = "none";
        	}
            $("#editBtn")[0].style.display = "none";
            $("#importVoucherBtn")[0].style.display = "none";
            $("#newVoucherBtn")[0].style.display = "none";
        }
        //组成数据表
        var columns = [];           //列数组
        var reg1 = RegExp(/金/);	//金额类型1
        var reg2 = RegExp(/价/);		//金额类型2
        var reg5 = RegExp(/费/);		//金额类型5
        var reg6 = RegExp(/余额/);	//金额类型6
        var reg3 = RegExp(/率/);		//floa类型
        var reg4 = RegExp(/日期/);	//日期类型
        
        if(reportName == "统计报表"){
        	console.log("统计报表");
        	//列名数组
            var columNameArr = [
				"zonghechaxun$合同号$合同号","zonghechaxun$业务员$业务员","zonghechaxun$合同名称$合同名称","zonghechaxun$签约金额$签约金额",
				"zonghechaxun$审定价$审定价","zonghechaxun$进款$进款","zonghechaxun$支出合计$支出合计","zonghechaxun$余额$余额","zonghechaxun$可支取余额$可支取余额",
				"zonghechaxun$间接费用$间接费用","zonghechaxun$对方单位$对方单位","zonghechaxun$开票额$开票额","zonghechaxun$小押金$小押金",
				"zonghechaxun$管理费$管理费","zonghechaxun$税费$税费","zonghechaxun$综合费率$综合费率","zonghechaxun$综合管理费$综合管理费",
				"zonghechaxun$材料费$材料费","zonghechaxun$人工费$人工费","zonghechaxun$机械使用费$机械使用费","zonghechaxun$其他直接费$其他直接费",
				"zonghechaxun$报表支出$报表支出","zonghechaxun$应收款$应收款","zonghechaxun$地区$地区","zonghechaxun$总分包$总分包",
				"zonghechaxun$合同类型$合同类型","zonghechaxun$合同日期$合同日期","zonghechaxun$标的价$标的价","zonghechaxun$本部合同$本部合同",
				"zonghechaxun$合同已到$合同已到","zonghechaxun$结账否$结账否","zonghechaxun$竣工否$竣工否"
			];
            for(var tjx = 0; tjx < columNameArr.length; tjx++){
            	var tjcolumnsObj = {};        //单个列
            	tjcolumnsObj.field = columNameArr[tjx];
            	tjcolumnsObj.width = 120;
            	tjcolumnsObj.header = columNameArr[tjx].split("$")[2];
            	tjcolumnsObj.headerAlign = "center";
            	tjcolumnsObj.align = "center";
                //columnsObj.allowSort = true;
                //判断如果filed字段有“金额”或者“价”的添加datatype="currency"属性
                if(reg1.test(tjcolumnsObj.field) || reg2.test(tjcolumnsObj.field) || reg5.test(tjcolumnsObj.field) || reg6.test(tjcolumnsObj.field)){
                	tjcolumnsObj.dataType = "currency";
                }
                //判断如果filed字段有“率”或者“价”的添加datatype="currency"属性
                if(reg3.test(tjcolumnsObj.field)){
                	tjcolumnsObj.dataType = "float";
                }
                //判断如果filed字段有“率”或者“价”的添加datatype="date"属性
                if(reg4.test(tjcolumnsObj.field)){
                	tjcolumnsObj.dataType = "date";
                }
                columns.push(tjcolumnsObj);
            }
            console.log(columns,"要插入的列");
        }else{
            for(var x = 0; x < columnCnType.length; x++){
                var columnsObj = {};        //单个列
                columnsObj.field = columnCnType[x].column_name;
                columnsObj.width = 120;
                columnsObj.header = columnCnType[x].column_name.split("$")[2];
                columnsObj.headerAlign = "center";
                columnsObj.align = "center";
                //columnsObj.allowSort = true;
                //判断如果filed字段有“金额”或者“价”的添加datatype="currency"属性
                if(reg1.test(columnsObj.field) || reg2.test(columnsObj.field) || reg5.test(columnsObj.field) || reg6.test(columnsObj.field)){
                     columnsObj.dataType = "currency";
                }
                //判断如果filed字段有“率”或者“价”的添加datatype="currency"属性
                if(reg3.test(columnsObj.field)){
                     columnsObj.dataType = "float";
                }
                //判断如果filed字段有“率”或者“价”的添加datatype="date"属性
                if(reg4.test(columnsObj.field)){
                    columnsObj.dataType = "date";
                }
                columns.push(columnsObj);
            }
            console.log(columns,"要插入的列");
        }
        //渲染数据表
        columns.unshift({ type: "indexcolumn" });
        grid.set({
        	allowSortColumn:true,		//false禁止列排序
        	title:reportName,
        	columns:columns
        });
        pager.set({"totalCount":pageCount});
        //数据表赋值
        //grid.setData(reportData);       
        var menu = new ColumnsMenu(grid);
        //查询不可用
        //searchBtn.disable();
        //Excel导出可用
        ExcelBtn.enable();
        
        //搜索框==========================================================================
        if(serchData.length > 0){
            //先追加一行
            $("#editForm1 table").append("<tr></tr>");
            //存放被替换后字符串得变量
            var inputArr = [];
            //数据绑定（拼接搜索框控件:5种类型）
            for(var n = 0; n < serchData.length; n++){
                switch(serchData[n].type){
                    case "1" :      //文本
                        replaceInput(inputList.str,serchData[n]);
                        break;
                    case "2" :      //日期
                        replaceInput(inputList.dateStr,serchData[n]);
                        break;
                    case "3" :      //文本区间
                        replaceInput(inputList.codeStr,serchData[n]);
                        break;
                    case "4" :      //日期区间
                        replaceInput(inputList.dateSection,serchData[n]);
                        break;
                    case "5" :      //下拉框
                        replaceInput(inputList.pullDown,serchData[n]);
                        break;
                }
            }
            //上树(DOM)
            var ss = 0;
            for(var s = 0; s < inputStrStrArr.length; s++){
                if(s < 4){
                    $("#editForm1 table tr").append(inputStrStrArr[s]);
                }else if(s >= 4){
                    if(s%4 == 0){
                        ss++;
                        $("#editForm1 table").append("<tr></tr>");
                    }
                    $("#editForm1 table tr").eq(ss).append(inputStrStrArr[s]);
                }
            }
            //页面显示miniui控件
            mini.parse();
        }else{
            //如果没有搜索条件
            $("#fieldsetBox").hide();
        }
    });

    //得到ajax返回数据的函数===============================================
    function getAjaxData(){
        //创建一个延迟对象
        var defer = $.Deferred();
        var obj = {
            reportId : childReportId,
            paramSql : "",
            sortFiledVal : "",
            sortOrder : ""
        }
        var jsonString = JSON.stringify(obj);
        $.ajax({
            url: ajaxUrl + "/reportController/listfirst.do",
            type:"post",
            contentType:"application/json",
            data : jsonString,
            success : function(data){
                //这个函数会在ajax执行完毕以后将参数data传递给回调方法
                defer.resolve(data);
            },
            error : function(msg){
                console.log(msg);
            }
        });
        //当ajax执行完毕的时候返回Deferred对象
        //它创建一个promise对象，其目的是在未来某个时间点返回一个响应
        return defer.promise();
    }
});
//分页
grid.on("beforeload", function (e) {
	//阻止本来的翻页功能
    e.cancel = true;   
    //当前页码
    var cunrrentPageIndex = grid.getPageIndex();  
    //点击按钮之后的页码
    var pageIndex = e.data.pageIndex; 
    //设置当前页数值
    pager.set({"pageIndex":pageIndex});
    //数据总数
    var pageCount = pubPageCount;
    pager.set({"pageIndex":pageIndex});
    //得到搜索框和输入的内容，将内容拼接成sql语句
    var formData = pubSerchData;
    var form = new mini.Form("form1");
    mini.parse();
    var dateStr = "";       //存放sql
    var dateArr = [];       //存放sql的数组
    for( var i = 0; i < formData.length; i++){
        var inputBoxs = mini.getByName(formData[i].dataSource + "1");
        //值不为空
        if(inputBoxs.getValue()){
            //如果是文本
            if(formData[i].type == "1" || formData[i].type == "5"){
                var colums = formData[i].dataSource;
                var colums1 = mini.getByName(formData[i].dataSource + "1").getValue();
                if(colums1 != ""){
                    dateStr = " and "  + colums + " like '%" + colums1 + "%'";
                    dateArr.push(dateStr);
                }else{
                    /*dateStr = " and "  + colums + " = " + colums;
                    dateArr.push(dateStr);*/
                }
            //如果是日期
            }else if(formData[i].type == "2"){
                var colums = formData[i].dataSource;
                var colums1 = mini.getByName(formData[i].dataSource + "1").getValue();
                if(colums1 != ""){
                    colums1 = mini.formatDate(colums1,"yyyy-MM-dd");       //时间格式
                    dateStr = " and CONVERT(varchar(100)," + colums +",23)='" + colums1 + "'";
                    dateArr.push(dateStr);
                }else{
                    /*dateStr = " and "  + colums + " = " + colums;
                    dateArr.push(dateStr);*/
                }
            //如果是文本区间
            }else if(formData[i].type == "3"){
                var colums= formData[i].dataSource;
                var colum1 = mini.getByName(formData[i].dataSource + "1").getValue();
                var colum2 = mini.getByName(formData[i].dataSource + "2").getValue();
                if(colum1 == "" && colum2 != ""){
                    dateStr = " and "  + colums + " < " + "'" + colum2 + "'";
                    dateArr.push(dateStr);
                }else if(colum1 != "" && colum2 == ""){
                	dateStr = " and "  + colums + " > " + "'" + colum1 + "'";
                    dateArr.push(dateStr);
                }else if(colum1 != "" && colum2 != ""){
                    if(colum1 < colum2){
                        dateStr = " and " + colums + " between '" + colum1 + "'" + " and '" + colum2 + "'";
                        dateArr.push(dateStr);
                    }else{
                        mini.alert("请输入正确的数字区间");
                    }
                }
            //如果是日期区间
            }else if(formData[i].type == "4"){
                var colums= formData[i].dataSource;
                //通过name得到日期区间得元素并且得到用户填写得值
                var colum1 = mini.getByName(formData[i].dataSource + "1").getValue();
                var colum2 = mini.getByName(formData[i].dataSource + "2").getValue();
                //如果值都不为空，那么执行sql语句
                if(colums1 != "" && colum2 != ""){
                    if(tab(colum1,colum2)){
                        colum1 = mini.formatDate(colum1,"yyyy-MM-dd");       //时间格式
                        colum2 = mini.formatDate(colum2,"yyyy-MM-dd");       //时间格式
                        dateStr = " and " + colums + " between '" + colum1 + "' and " + "'" + colum2 + "'";
                        dateArr.push(dateStr);
                    }else{
                        mini.alert("第二个日期不能大于以一个日期");
                    }
                }else if(colums1 == "" && colum2 != ""){
                    colum2 = mini.formatDate(colum2,"yyyy-MM-dd");       //时间格式
                    dateStr = " and "  + colums + " <= " + "'" + colum2 + "'";
                    dateArr.push(dateStr);
                }else if(colums1 != "" && colum2 == ""){
                    colum1 = mini.formatDate(colum1,"yyyy-MM-dd");       //时间格式
                	dateStr = " and "  + colums + " >= " + "'" + colum1 + "'";
                    dateArr.push(dateStr);
                }
            }else if(formData[i].type == "5"){
                var colums = formData[i].dataSource;
                var colums1 = mini.getByName(formData[i].dataSource + "1").getValue();
                if(colums1 != ""){
                    dateStr = " and "  + colums + " like '%" + colums1 + "%'";
                    dateArr.push(dateStr);
                }else{
                    /*dateStr = " and "  + colums + " = " + colums;
                     dateArr.push(dateStr);*/
                }
                //如果是日期
            }
        }else{

        }
    }
    var searchSql = dateArr.join("");
    var sortFiledVal = sortFiledInput.getValue();
	var sortOrder = ascDesc.getValue();
    var searchSqlData = {
        reportId : childReportId,
        paramSql : searchSql,
        pageNum : pageIndex,
        sortFiledVal : sortFiledVal,
        sortOrder : sortOrder
    }
    console.log(searchSqlData,"分页条件");
    var jsonString = JSON.stringify(searchSqlData);
    grid.loading("加载中，请稍后......");
    //重新显示查询后的数据
    $.ajax({
        url: ajaxUrl + "/reportController/list.do",
        type:"post",
        contentType:"application/json",
        data : jsonString,
        success: function (data) {
            var sourse = data.data;
            console.log(sourse,"翻页后返回的数据");
            var pageCounts = data.pageCount[0].id;
            grid.setData(sourse);
            pager.set({"totalCount":pageCounts});        //设置分页总数
            pager.set({"pageIndex":pageIndex});
        },
        error: function () {
            mini.alert('失败');
        }
    });
});
//下拉框赋值====================================
function pullDown(e){
    var element = e;
    var columnNames = e.id;
    var tableNames = columnNames.split(".")[0];
    //下拉框点击之前
    e.on("beforeshowpopup",function(e){
        //得到元素
        var elePopup =  e.sender;
        //如果没有下拉数据
        if(!elePopup.getData().length > 0){
            //组织弹框弹出
            e.cancel=true;
            //请求数据
            $.ajax({
                url : ajaxUrl + "/reportController/Reference.do",
                type : "POST",
                data : {
                    reportId : childReportId,
                    tableName : tableNames,
                    columnName : columnNames
                },
                success : function(data){
                    var dropDownData = data.data;
                    var pullKeys;
                    for(var keys in dropDownData[dropDownData.length-1]){
                        pullKeys = keys;
                    }
                    var dropDownArr = [];      //存放下拉框数据对象的数组
                    for(var r = 0; r < dropDownData.length; r++){
                        var dropDownObj = {};      //存放下拉框数据得对象
                        dropDownObj.id = dropDownData[r][pullKeys] ? dropDownData[r][pullKeys] : "";
                        dropDownObj.text = dropDownData[r][pullKeys] ? dropDownData[r][pullKeys] : "";
                        dropDownArr.push(dropDownObj);
                    }
                    var eles = mini.get(columnNames);
                    //给下拉框赋值
                    eles.set({
                        data : dropDownArr
                    });
                    //赋值以后弹出弹框
                    e.cancel=false;
                    elePopup.showPopup();
                },
                error : function(msg){
                    console.log(msg);
                }
            });
        }else{

        }
    });
}
var nns = 0;		//函数执行次数初始化
//查询
function search(){
	nns++;
	console.log("函数执行了" + nns + "次");
	//搜索框输入内容
	var form = new mini.Form("form1");
	var sortFiledVal = sortFiledInput.getValue();
	var sortOrder = ascDesc.getValue();
	//当前页码
    var nowPageIdx = grid.getPageIndex(); 
    var searchSql = searchSQL();
    var searchSqlData = {
        reportId : childReportId,
        paramSql : searchSql,
        pageSize : 500,
        sortFiledVal : sortFiledVal,
        sortOrder : sortOrder
    }
    console.log(searchSqlData,"sql");
    var jsonString = JSON.stringify(searchSqlData);
    grid.loading("加载中，请稍后......");
    //查询数据
    $.ajax({
        url: ajaxUrl + "/reportController/list.do",
        type:"post",
        contentType:"application/json",
        data : jsonString,
        success: function (data) {
        	console.log(data,"查询返回后的数据");
        	//设置当前页数值======================
            pager.set({"pageIndex":0});
        	//数据表的数据========================
            var reportData = data.data;                     //表格列数据/内容数据
            grid.setData(reportData);						//数据赋值
            //总数值显示=========================================================================
            var countData = data.countDate;
            //根据countDate的长度判断数据表显示的大小
            var cColumns = [];
            if(countData && countData[0]){
            	grid.style += "height:90%";
            	countGrid.height="66px";
            	console.log(countGrid);
                for(var k in countData[0]){
                    var countColumns = {};
                    countColumns.field = k;
                    countColumns.width = 120;
                    countColumns.header = k;
                    countColumns.align = "center";
                    countColumns.headerAlign = "center";
                    cColumns.push(countColumns);
                }
                countGrid.set({columns:cColumns});
                countGrid.setData(countData);
            }else{
                $("#countGrid").hide();
            }
            var pageCount = data.pageCount[0].id;          	//总数
            pager.set({totalCount:pageCount});				//总数赋值
            //pager.set({"pageIndex":0});
        },
        error: function () {
            mini.alert('失败');
        }
    });
}
//excel导出
function excelExport() {
	var searchSql = searchSQL();
    var nowPageIdx = grid.getPageIndex();  //当前页码
    var searchSqlData = {
    		reportId: childReportId,
            paramSql: searchSql,
            sortFiledVal : "",
            sortOrder : ""
    }
    var jsonString = JSON.stringify(searchSqlData);
    grid.loading("加载中，请稍后......");
    $.ajax({
    	url: ajaxUrl + "/reportController/excelExport.do",
    	type: "POST",
    	data: jsonString,
    	contentType: "application/json",
    	success: function (data) {
    		
    		grid.unmask();
    		excelData = data.data; 
    		var titleArr = data.columnCnType;	//列名
    		var titleStr;
    		//导出对齐
    		if (data.reportParam.reportName == "开票查询") {
    			titleStr = ifExcel(titleArr,excelData);
    		} else if (data.reportParam.reportName == "合同查询") {
    			titleStr = hetongExcel(titleArr,excelData);
    		}else if(data.reportParam.reportName == "进款明细"){
    			titleStr = jinkuanExcel(titleArr,excelData);
    		}else if(data.reportParam.reportName == "支出明细"){
    			titleStr = zhichuExcel(titleArr,excelData);
    		}else if(data.reportParam.reportName == "统计报表"){
    			titleStr = tongjiExcel(titleArr,excelData);
    		}
    		//encodeURIComponent解决中文乱码
    		var uri = 'data:text/csv;charset=utf-8,\ufeff' + encodeURIComponent(titleStr);
    		//通过创建a标签实现
    		var link = document.createElement("a");
    		link.href = uri;
    		//对下载的文件命名
    		link.download = "数据.csv";
    		document.body.appendChild(link);
    		link.click();
    		document.body.removeChild(link);
    	},
    	error: function (data) {
    		console.log(data);
    	}
    });
    var excelData = grid.getData();
    //列标题，逗号隔开，每一个逗号就是隔开一个单元格
    var titleArr = [];
    //遍历key（输出标题名字）
    for (var key in excelData[0]) {
    	titleArr.push(key.split("$")[2]);
    }
}
//合同修改
function editContract(){
	var row = grid.getSelected();
	if(row){
		mini.open({
	        url: url +"alertBox/editContract.html",
	        title: "修改合同", 
	        width: 800,
            height: 500,
	        onload: function () {
	            var iframe = this.getIFrameEl();
	            var data = { 
	            		action: "edit",
	            		contractCode : row.HeTongView$合同号$合同号,
                        contractName : row.HeTongView$合同名称$合同名称
	            };
	            iframe.contentWindow.SetData(data);
	        },
	        ondestroy: function (action) {
                if(action == "save"){
                	grid.reload();
                }
	        }
	    });
	}else{
		mini.alert("请选择一条");
	}
}
//新增凭证
function newVoucher(){
	var row = grid.getSelected();
	mini.open({
		url: url +"alertBox/addVoucher.html",
		title: "新增凭证",
		width: 600,
		height: 400,
		onload: function () {
			var iframe = this.getIFrameEl();
			var data;
			//如果选中
			if(row){
				data = {
						action 		 : "add",
						parReportId	 : childReportId,
						salesman 	 : row.zhichumingxi$业务员$业务员,
						contractCode : row.zhichumingxi$合同号$合同号,
						contractName : row.zhichumingxi$合同名称$合同名称,
						cPersonCode  : row.zhichumingxi$业务员编码$业务员编码
				};
			//如果未选中
			}else{
				data = {
						action : "add",
						parReportId: childReportId,
				};
			}
			iframe.contentWindow.SetData(data);
		},
		ondestroy: function (action) {
			console.log(action);
			if(action == "save"){
				grid.reload();
				var obj = {
						reportId : childReportId,
						paramSql : ""
				}
				var jsonString = JSON.stringify(obj);
				$.ajax({
					url: ajaxUrl + "/reportController/list.do",
					type:"post",
					contentType:"application/json",
					data : jsonString,
					success : function(data){
						grid.setData(data.data);
					},
					error : function(msg){
						console.log(msg);
					}
				});
			}
		}
	});
}
//导入凭证
function importVoucher(){
	//打开新页面
    mini.open({
        url: url +"alertBox/ImportVoucher.html",
        showMaxButton: true,
        title: "导入凭证",
        width: "95%",
        height: "93%",
        onload: function () {
            var iframe = this.getIFrameEl();
            var data = {
                action: "import"
            };
            iframe.contentWindow.SetData(data);
        },
        ondestroy: function (action) {
            if(action == "save"){
            	console.log("保存成功");
//            	var iframe = this.getIFrameEl();
//                var childData1 = iframe.contentWindow.GetData();
//                //var childData1 = getData();
//                console.log(childData1);
//                row.setData(childData1);
            }
        }
    });
}
//开票查询
function ifExcel(titleArr,excelData){
	//标题列表数组
	var titleTextArr = ["录入时间", "结算单号", "合同号", "凭证号", "金额", "开票地域", "发票类型", "发票费率", "综合费率", "综合税费", "业务员"]
    //标题列表字符串
    var titleStr = titleTextArr.join();
    titleStr += "\n";
	//将数据按2000分组
	var excelDatasArr = SplitArray (2000,excelData);
	for(var j = 0; j < excelDatasArr.length; j++){
		for(var i = 0; i < excelDatasArr[j].length; i++){
			//excelDatasArr[j][i]["kaipiaomingxi$录入时间$录入时间"]
		    if (!excelDatasArr[j][i]["kaipiaomingxi$录入时间$录入时间"]) {
		        titleStr += '  ,';
		    } else {
		    	var tjlrsjStr = excelDatasArr[j][i]["kaipiaomingxi$录入时间$录入时间"].replace(/[\r\n\↵]/g,"");
		        titleStr += tjlrsjStr + '\t,';
		    }
		    //excelDatasArr[j][i]["kaipiaomingxi$结算单号$结算单号"]
		    if (!excelDatasArr[j][i]["kaipiaomingxi$结算单号$结算单号"]) {
		        titleStr += '  ,';
		    } else {
		    	var tjjsdhStr = excelDatasArr[j][i]["kaipiaomingxi$结算单号$结算单号"].replace(/[\r\n\↵]/g,"");
		        titleStr += tjjsdhStr + '\t,';
		    }
		    //excelDatasArr[j][i]["kaipiaomingxi$合同号$合同号"]
		    if (!excelDatasArr[j][i]["kaipiaomingxi$合同号$合同号"]) {
		        titleStr += '  ,';
		    } else {
		    	var tjhthStr = excelDatasArr[j][i]["kaipiaomingxi$合同号$合同号"].replace(/[\r\n\↵]/g,"");
		        titleStr += tjhthStr + '\t,';
		    }
		    //excelDatasArr[j][i]["kaipiaomingxi$凭证号$凭证号"]
		    if (!excelDatasArr[j][i]["kaipiaomingxi$凭证号$凭证号"]){
		        titleStr += '  ,';
		    } else {
		    	var tjpzhStr = (excelDatasArr[j][i]["kaipiaomingxi$凭证号$凭证号"]+"").replace(/[\r\n\↵]/g,"");
		        titleStr += tjpzhStr + '\t,';
		    }
		    //excelDatasArr[j][i]["kaipiaomingxi$金额$金额"]
		    if (excelDatasArr[j][i]["kaipiaomingxi$金额$金额"] == "") {
		        titleStr += '  ,';
		    } else {
		    	if(excelDatasArr[j][i]["kaipiaomingxi$金额$金额"]){
		    		var kpje = (excelDatasArr[j][i]["kaipiaomingxi$金额$金额"] + "").replace(/,/g,"");
		            titleStr += kpje + '\t,';
		    	}else{
		    		titleStr += '  ,';
		    	}
		    }
		    //excelDatasArr[j][i]["kaipiaomingxi$开票地域$开票地域"]
		    if (!excelDatasArr[j][i]["kaipiaomingxi$开票地域$开票地域"]) {
		        titleStr += '  ,';
		    } else {
		    	var tjkpdyStr = excelDatasArr[j][i]["kaipiaomingxi$开票地域$开票地域"].replace(/[\r\n\↵]/g,"");
		        titleStr += tjkpdyStr + '\t,';
		    }
		    //excelDatasArr[j][i]["kaipiaomingxi$发票类型$发票类型"]
		    if (!excelDatasArr[j][i]["kaipiaomingxi$发票类型$发票类型"]) {
		        titleStr += '   ,';
		    } else {
		    	var tjfplxStr = excelDatasArr[j][i]["kaipiaomingxi$发票类型$发票类型"].replace(/[\r\n\↵]/g,"");
		        titleStr += tjfplxStr + '\t,';
		    }
		    //excelDatasArr[j][i]["kaipiaomingxi$发票费率$发票费率"]
		    if (!excelDatasArr[j][i]["kaipiaomingxi$发票费率$发票费率"]) {
		        titleStr += '  ,';
		    } else {
		    	var tjfpflStr = (excelDatasArr[j][i]["kaipiaomingxi$发票费率$发票费率"]+"").replace(/[\r\n\↵]/g,"");
		        titleStr += tjfpflStr + '\t,';
		    }
		    //excelDatasArr[j][i]["kaipiaomingxi$综合费率$综合费率"]
		    if (!excelDatasArr[j][i]["kaipiaomingxi$综合费率$综合费率"]) {
		        titleStr += '  ,';
		    } else {
		    	var tjzhflStr = (excelDatasArr[j][i]["kaipiaomingxi$综合费率$综合费率"]+"").replace(/[\r\n\↵]/g,"");
		        titleStr += tjzhflStr + '\t,';
		    }
		    //excelDatasArr[j][i]["kaipiaomingxi$综合税费$综合税费"]
		    if (!excelDatasArr[j][i]["kaipiaomingxi$综合税费$综合税费"]) {
		        titleStr += '  ,';
		    } else {
		    	var tjzhsfStr = (excelDatasArr[j][i]["kaipiaomingxi$综合税费$综合税费"]+"").replace(/[\r\n\↵]/g,"").replace(/\,/g,"");
		        titleStr += tjzhsfStr + '\t,';
		    }
		    //excelDatasArr[j][i]["kaipiaomingxi$业务员$业务员"]
		    if (!excelDatasArr[j][i]["kaipiaomingxi$业务员$业务员"]) {
		        titleStr += '  ,';
		    } else {
		    	var tjywyStr = excelDatasArr[j][i]["kaipiaomingxi$业务员$业务员"].replace(/[\r\n\↵]/g,"");
		        titleStr += tjywyStr + '\t,';
		    }
		    titleStr += '\n';
		}
	}
    return titleStr;
}    
//合同
function hetongExcel(titleArr,excelData){
	//标题列表数组
	var titleTextArr = ["合同号", "合同类型", "合同名称", "对方单位", "签约金额", "合同日期", "综合费率", 
		"发票税率", "管理费率", "录入人", "收付类型", "审定价", "地区", "标的价", "部门", "业务员", 
		"竣工否", "结账否", "备注", "总分包", "合同已到", "本部合同", "合同收取日期", "竣工报告日期"]
//    //标题列表数组
//    for (var t = 0; t < titleArr.length; t++) {
//        titleTextArr.push(titleArr[t].column_name.split("$")[2]);
//    }
    //标题列表字符串
    var titleStr = titleTextArr.join();
    titleStr += "\n";
    //将数据按2000分组
	var excelDatasArr = SplitArray (2000,excelData);
	for(var j = 0; j < excelDatasArr.length; j++){
		for(var i = 0; i < excelDatasArr[j].length; i++){
	        if (!excelDatasArr[j][i]["HeTongView$合同号$合同号"]) {
	            titleStr += '  ,';
	        } else {
	        	var hthStr = excelDatasArr[j][i]["HeTongView$合同号$合同号"].replace(/[\r\n\↵]/g,"");
	            titleStr += hthStr + '\t,';
	        }
	        //excelDatasArr[j][i]["HeTongView$合同类型$合同类型"]
	        if (!excelDatasArr[j][i]["HeTongView$合同类型$合同类型"]) {
	            titleStr += '  ,';
	        } else {
	        	var htlxStr = excelDatasArr[j][i]["HeTongView$合同类型$合同类型"].replace(/[\r\n\↵]/g,"");
	            titleStr += htlxStr + '\t,';
	        }
	        //excelDatasArr[j][i]["HeTongView$合同名称$合同名称"]
	        if (!excelDatasArr[j][i]["HeTongView$合同名称$合同名称"]) {
	            titleStr += '  ,';
	        } else {
	        	var htmcStr = (excelDatasArr[j][i]["HeTongView$合同名称$合同名称"].replace(/,/g,'.')).replace(/[\r\n\↵]/g,"");
	            titleStr += htmcStr + '\t,';
	        }
	        //excelDatasArr[j][i]["HeTongView$对方单位$对方单位"]
	        if (!excelDatasArr[j][i]["HeTongView$对方单位$对方单位"]) {
	            titleStr += '  ,';
	        } else {
	        	var dfdwStr = (excelDatasArr[j][i]["HeTongView$对方单位$对方单位"].replace(/,/g,'.')).replace(/[\r\n\↵]/g,"");
	            titleStr += dfdwStr + '\t,';
	        }
	        //excelDatasArr[j][i]["HeTongView$签约金额$签约金额"]
	        if (!excelDatasArr[j][i]["HeTongView$签约金额$签约金额"]) {
	            titleStr += '  ,';
	        } else {
	        	var qyje = excelDatasArr[j][i]["HeTongView$签约金额$签约金额"].replace(/,/g,"");
                titleStr += qyje + '\t,';
	        }
	        //excelDatasArr[j][i]["HeTongView$合同日期$合同日期"]
	        if (!excelDatasArr[j][i]["HeTongView$合同日期$合同日期"]) {
	            titleStr += '  ,';
	        } else {
	        	var htrqStr = excelDatasArr[j][i]["HeTongView$合同日期$合同日期"].replace(/[\r\n\↵]/g,"");
	            titleStr += htrqStr + '\t,';
	        }
	        //excelDatasArr[j][i]["HeTongView$综合费率$综合费率"]
	        if (!excelDatasArr[j][i]["HeTongView$综合费率$综合费率"]) {
	        	titleStr += '  ,';
	        } else {
	        	var htzhStr = (excelDatasArr[j][i]["HeTongView$综合费率$综合费率"]+"").replace(/[\r\n\↵]/g,"");
	            titleStr += htzhStr + '\t,';
	        }
	        //excelDatasArr[j][i]["HeTongView$发票税率$发票税率"]
	        if (!excelDatasArr[j][i]["HeTongView$发票税率$发票税率"]) {
	            titleStr += '  ,';
	        } else {
	        	var htfpStr = (excelDatasArr[j][i]["HeTongView$发票税率$发票税率"]+"").replace(/[\r\n\↵]/g,"");
	            titleStr += htfpStr + '\t,';
	        }
	        //excelDatasArr[j][i]["HeTongView$管理费率$管理费率"]
	        if (!excelDatasArr[j][i]["HeTongView$管理费率$管理费率"]) {
	            titleStr += '  ,';
	        } else {
	        	var htglStr = (excelDatasArr[j][i]["HeTongView$管理费率$管理费率"]+"").replace(/[\r\n\↵]/g,"");
	            titleStr += htglStr + '\t,';
	        }
	        //excelDatasArr[j][i]["HeTongView$录入人$录入人"]
	        if (!excelDatasArr[j][i]["HeTongView$录入人$录入人"]) {
	            titleStr += '  ,';
	        } else {
	        	var htlrrStr = excelDatasArr[j][i]["HeTongView$录入人$录入人"].replace(/[\r\n\↵]/g,"");
	            titleStr += htlrrStr + '\t,';
	        }
	        //excelDatasArr[j][i]["HeTongView$收付类型$收付类型"]
	        if (!excelDatasArr[j][i]["HeTongView$收付类型$收付类型"]) {
	            titleStr += '  ,';
	        } else {
	        	var htsfStr = excelDatasArr[j][i]["HeTongView$收付类型$收付类型"].replace(/[\r\n\↵]/g,"");
	            titleStr += htsfStr + '\t,';
	        }
	        //excelDatasArr[j][i]["HeTongView$审定价$审定价"]
	        if (!excelDatasArr[j][i]["HeTongView$审定价$审定价"]) {
	            titleStr += '  ,';
	        } else {
	        	var htsdjNum = excelDatasArr[j][i]["HeTongView$审定价$审定价"];
	        	var htsdjStr = htsdjNum.replace(/,/g,'');
                titleStr += htsdjStr + '\t,';
	        }
	        //excelDatasArr[j][i]["HeTongView$地区$地区"]
	        if (!excelDatasArr[j][i]["HeTongView$地区$地区"]) {
	            titleStr += '  ,';
	        } else {
	        	var htdqStr = excelDatasArr[j][i]["HeTongView$地区$地区"].replace(/[\r\n\↵]/g,"");
	            titleStr += htdqStr + '\t,';
	        }
	        //excelDatasArr[j][i]["HeTongView$标的价$标的价"]
	        if (!excelDatasArr[j][i]["HeTongView$标的价$标的价"]) {
	            titleStr += '  ,';
	        } else {
	        	var bdj = excelDatasArr[j][i]["HeTongView$标的价$标的价"].replace(/,/g,'');
                titleStr += bdj + '\t,';
	        }
	        //excelDatasArr[j][i]["HeTongView$部门$部门"]
	        if (!excelDatasArr[j][i]["HeTongView$部门$部门"]) {
	            titleStr += '  ,';
	        } else {
	        	var htbmStr = excelDatasArr[j][i]["HeTongView$部门$部门"].replace(/[\r\n\↵]/g,"");
	            titleStr += htbmStr + '\t,';
	        }
	        //excelDatasArr[j][i]["HeTongView$业务员$业务员"]
	        if (!excelDatasArr[j][i]["HeTongView$业务员$业务员"]) {
	            titleStr += '  ,';
	        } else {
	        	var htywyStr = excelDatasArr[j][i]["HeTongView$业务员$业务员"].replace(/[\r\n\↵]/g,"");
	            titleStr += htywyStr + '\t,';
	        }
	        //excelDatasArr[j][i]["HeTongView$竣工否$竣工否"]
	        if (!excelDatasArr[j][i]["HeTongView$竣工否$竣工否"]) {
	            titleStr += '  ,';
	        } else {
	        	var htjgStr = excelDatasArr[j][i]["HeTongView$竣工否$竣工否"].replace(/[\r\n\↵]/g,"");
	            titleStr += htjgStr + '\t,';
	        }
	        //excelDatasArr[j][i]["HeTongView$结账否$结账否"]
	        if (!excelDatasArr[j][i]["HeTongView$结账否$结账否"]) {
	            titleStr += '  ,';
	        } else {
	        	var htjzStr = excelDatasArr[j][i]["HeTongView$结账否$结账否"].replace(/[\r\n\↵]/g,"");
	            titleStr += htjzStr + '\t,';
	        }
	        //excelDatasArr[j][i]["HeTongView$备注$备注"]
	        if (!excelDatasArr[j][i]["HeTongView$备注$备注"]) {
	            titleStr += '  ,';
	        } else {
	        	var bzStr = (excelDatasArr[j][i]["HeTongView$备注$备注"].replace(/,/g,'，')).replace(/[\r\n\↵]/g,"");
	            titleStr += bzStr + '\t,';
	        }
	        //excelDatasArr[j][i]["HeTongView$总分包$总分包"]
	        if(!excelDatasArr[j][i]["HeTongView$总分包$总分包"]) {
	            titleStr += '  ,';
	        }else{
	        	var htfbStr = excelDatasArr[j][i]["HeTongView$总分包$总分包"].replace(/[\r\n\↵]/g,"");
	            titleStr += htfbStr + '\t,';
	        }
	        //excelDatasArr[j][i]["HeTongView$合同已到$合同已到"]
	        if(!excelDatasArr[j][i]["HeTongView$合同已到$合同已到"]) {
	            titleStr += '  ,';
	        }else{
	        	var htydStr = excelDatasArr[j][i]["HeTongView$合同已到$合同已到"].replace(/[\r\n\↵]/g,"");
	            titleStr += htydStr + '\t,';
	        }
	        //excelDatasArr[j][i]["HeTongView$本部合同$本部合同"]
	        if(!excelDatasArr[j][i]["HeTongView$本部合同$本部合同"]) {
	            titleStr += '  ,';
	        }else{
	        	var htbbStr = excelDatasArr[j][i]["HeTongView$本部合同$本部合同"].replace(/[\r\n\↵]/g,"");
	            titleStr += htbbStr + '\t,';
	        }
	        //excelDatasArr[j][i]["HeTongView$合同收取日期$合同收取日期"]
	        if (!excelDatasArr[j][i]["HeTongView$合同收取日期$合同收取日期"]) {
	            titleStr += '  ,';
	        }else{
	        	if(excelDatasArr[j][i]["HeTongView$合同收取日期$合同收取日期"]);
	        	var htsqrqStr = excelDatasArr[j][i]["HeTongView$合同收取日期$合同收取日期"].replace(/[\r\n\↵]/g,"");
	            titleStr += htsqrqStr + '\t,';
	        }
	        //excelDatasArr[j][i]["HeTongView$竣工报告日期$竣工报告日期"]
	        if(!excelDatasArr[j][i]["HeTongView$竣工报告日期$竣工报告日期"]) {
	            titleStr += '  ,';
	        }else{
	        	var htjgrqrqStr = excelDatasArr[j][i]["HeTongView$竣工报告日期$竣工报告日期"].replace(/[\r\n\↵]/g,"");
	            titleStr += htjgrqrqStr + '\t,';
	        }
	        //======
	        titleStr += '\n';
	    }
	}
    return titleStr;
}
//进款明细
function jinkuanExcel(titleArr,excelData){
	//标题列表数组
	var titleTextArr = ["合同号", "业务员", "部门", "合同类型", "合同名称", 
		"单据日期", "单据号", "凭证号", "金额", "本部合同", "竣工否", "管理费", "对方单位"];
//    //标题列表数组
//    for (var t = 0; t < titleArr.length; t++) {
//        titleTextArr.push(titleArr[t].column_name.split("$")[2]);
//    }
    //标题列表字符串
    var titleStr = titleTextArr.join();
    titleStr += "\n";
    for(var i = 0; i < excelData.length; i++){
    	//excelData[i]["jinkuanmingxi$合同号$合同号"]
        if (!excelData[i]["jinkuanmingxi$合同号$合同号"]) {
            titleStr += '  ,';
        } else {
        	var jkhthStr = excelData[i]["jinkuanmingxi$合同号$合同号"].replace(/[\r\n\↵]/g,"");
            titleStr += jkhthStr + '\t,';
        }
        //excelData[i]["jinkuanmingxi$业务员$业务员"]
        if (!excelData[i]["jinkuanmingxi$业务员$业务员"]) {
            titleStr += '  ,';
        } else {
        	var jkywyStr = excelData[i]["jinkuanmingxi$业务员$业务员"].replace(/[\r\n\↵]/g,"");
            titleStr += jkywyStr + '\t,';
        }
        //excelData[i]["jinkuanmingxi$部门$部门"]
        if (!excelData[i]["jinkuanmingxi$部门$部门"]) {
            titleStr += '  ,';
        } else {
        	var jkbmStr = excelData[i]["jinkuanmingxi$部门$部门"].replace(/[\r\n\↵]/g,"");
            titleStr += jkbmStr + '\t,';
        }
        //excelData[i]["jinkuanmingxi$合同类型$合同类型"]
        if (!excelData[i]["jinkuanmingxi$合同类型$合同类型"]) {
            titleStr += '  ,';
        } else {
        	var jkhtlxtr = excelData[i]["jinkuanmingxi$合同类型$合同类型"].replace(/[\r\n\↵]/g,"");
            titleStr += jkhtlxtr + '\t,';
        }
        //excelData[i]["jinkuanmingxi$合同名称$合同名称"]
        if (!excelData[i]["jinkuanmingxi$合同名称$合同名称"]) {
            titleStr += '  ,';
        } else {
        	var jkhtmcStr = (excelData[i]["jinkuanmingxi$合同名称$合同名称"].replace(/,/g,'.')).replace(/[\r\n\↵]/g,"");
            titleStr += jkhtmcStr + '\t,';
        }
        //excelData[i]["jinkuanmingxi$单据日期$单据日期"]
        if (!excelData[i]["jinkuanmingxi$单据日期$单据日期"]) {
            titleStr += '  ,';
        } else {
        	var jkdjrqStr = excelData[i]["jinkuanmingxi$单据日期$单据日期"].replace(/[\r\n\↵]/g,"");
            titleStr += jkdjrqStr + '\t,';
        }
        //excelData[i]["jinkuanmingxi$单据号$单据号"]
        if (!excelData[i]["jinkuanmingxi$单据号$单据号"]) {
            titleStr += '  ,';
        } else {
        	var jkdjhStr = (excelData[i]["jinkuanmingxi$单据号$单据号"]+"").replace(/[\r\n\↵]/g,"");
            titleStr += jkdjhStr + '\t,';
        }
        //excelData[i]["jinkuanmingxi$凭证号$凭证号"]
        if (!excelData[i]["jinkuanmingxi$凭证号$凭证号"]) {
            titleStr += '  ,';
        } else {
        	var jkpzhStr = (excelData[i]["jinkuanmingxi$凭证号$凭证号"]+"").replace(/[\r\n\↵]/g,"");
            titleStr += jkpzhStr + '\t,';
        }
        //excelData[i]["jinkuanmingxi$金额$金额"]
        if (!excelData[i]["jinkuanmingxi$金额$金额"]) {
            titleStr += '  ,';
        } else {
        	if(excelData[i]["jinkuanmingxi$金额$金额"]){
        		var jkje = excelData[i]["jinkuanmingxi$金额$金额"].replace(/,/g,'');
                titleStr += jkje + '\t,';
        	}else{
        		titleStr += '  ,';
        	}
        }
        //excelData[i]["jinkuanmingxi$本部合同$本部合同"]
        if (!excelData[i]["jinkuanmingxi$本部合同$本部合同"]) {
            titleStr += '  ,';
        } else {
        	var jkbbhtStr = excelData[i]["jinkuanmingxi$本部合同$本部合同"].replace(/[\r\n\↵]/g,"");
            titleStr += jkbbhtStr + '\t,';
        }
        //excelData[i]["jinkuanmingxi$竣工否$竣工否"]
        if (!excelData[i]["jinkuanmingxi$竣工否$竣工否"]) {
            titleStr += '  ,';
        } else {
        	var jkjgfStr = excelData[i]["jinkuanmingxi$竣工否$竣工否"].replace(/[\r\n\↵]/g,"");
            titleStr += jkjgfStr + '\t,';
        }
        //excelData[i]["jinkuanmingxi$管理费$管理费"]
        if (!excelData[i]["jinkuanmingxi$管理费$管理费"]) {
            titleStr += ' 空,';
        } else {
        	var jkglfStr = ((excelData[i]["jinkuanmingxi$管理费$管理费"]+"").replace(/,/g,'')).replace(/[\r\n\↵]/g,"");
            titleStr += jkglfStr + '\t,';
        }
        //excelData[i]["jinkuanmingxi$对方单位$对方单位"]
        if (!excelData[i]["jinkuanmingxi$对方单位$对方单位"]) {
            titleStr += '  ,';
        } else {
        	var jkdfdwStr = (excelData[i]["jinkuanmingxi$对方单位$对方单位"].replace(/,/g,'.')).replace(/[\r\n\↵]/g,"");
            titleStr += jkdfdwStr + '\t,';
        }
        //======
        titleStr += '\n';
    }
    return titleStr;
}
//支出明细
function zhichuExcel(titleArr,excelData){
	//标题列表数组
	var titleTextArr = ["合同号", "业务员", "合同名称", "供应商", "合同类型", "单据日期", "单据号",
		"凭证号", "金额", "录入人", "支出类别", "具体描述", "数据来源", "本部合同", "业务员编码"];
    //标题列表数组
//    for (var t = 0; t < titleArr.length; t++) {
//        titleTextArr.push(titleArr[t].column_name.split("$")[2]);
//    }
    //标题列表字符串
    var titleStr = titleTextArr.join();
    titleStr += "\n";
    for(var i = 0; i < excelData.length; i++){
    	//excelData[i]["zhichumingxi$合同号$合同号"]
        if (!excelData[i]["zhichumingxi$合同号$合同号"]) {
            titleStr += '  ,';
        } else {
        	var zchthStr = excelData[i]["zhichumingxi$合同号$合同号"].replace(/[\r\n\↵]/g,"");
            titleStr += zchthStr + '\t,';
        }
        //excelData[i]["zhichumingxi$业务员$业务员"]
        if (!excelData[i]["zhichumingxi$业务员$业务员"]) {
            titleStr += '  ,';
        } else {
        	var zcywyStr = excelData[i]["zhichumingxi$业务员$业务员"].replace(/[\r\n\↵]/g,"");
            titleStr += zcywyStr + '\t,';
        }
        //excelData[i]["zhichumingxi$合同名称$合同名称"]
        if (!excelData[i]["zhichumingxi$合同名称$合同名称"]) {
            titleStr += '  ,';
        } else {
        	var zchtmcStr = (excelData[i]["zhichumingxi$合同名称$合同名称"].replace(/,/g,'，')).replace(/[\r\n\↵]/g,"");
            titleStr += zchtmcStr + '\t,';
        }
        //excelData[i]["zhichumingxi$供应商$供应商"]
        if (!excelData[i]["zhichumingxi$供应商$供应商"]) {
            titleStr += '  ,';
        } else {
        	var zcgysStr = excelData[i]["zhichumingxi$供应商$供应商"].replace(/[\r\n\↵]/g,"");
            titleStr += zcgysStr + '\t,';
        }
        //excelData[i]["zhichumingxi$合同类型$合同类型"]
        if (!excelData[i]["zhichumingxi$合同类型$合同类型"]) {
            titleStr += '  ,';
        } else {
        	var zchtlxStr = excelData[i]["zhichumingxi$合同类型$合同类型"].replace(/[\r\n\↵]/g,"");
            titleStr += zchtlxStr + '\t,';
        }
        //excelData[i]["jinkuanmingxi$单据日期$单据日期"]
        if (!excelData[i]["zhichumingxi$单据日期$单据日期"]) {
            titleStr += '  ,';
        } else {
        	var zcdjrqStr = excelData[i]["zhichumingxi$单据日期$单据日期"].replace(/[\r\n\↵]/g,"");
            titleStr += zcdjrqStr + '\t,';
        }
        //excelData[i]["zhichumingxi$单据号$单据号"]
        if (!excelData[i]["zhichumingxi$单据号$单据号"]) {
            titleStr += '  ,';
        } else {
        	var zcdjhStr = excelData[i]["zhichumingxi$单据号$单据号"].replace(/[\r\n\↵]/g,"");
            titleStr += zcdjhStr + '\t,';
        }
        //excelData[i]["zhichumingxi$凭证号$凭证号"]
        if (!excelData[i]["zhichumingxi$凭证号$凭证号"]) {
            titleStr += '  ,';
        } else {
        	var zcpzhStr = (excelData[i]["zhichumingxi$凭证号$凭证号"]+"").replace(/[\r\n\↵]/g,"");
            titleStr += zcpzhStr + '\t,';
        }
        //excelData[i]["zhichumingxi$金额$金额"]
        if (!excelData[i]["zhichumingxi$金额$金额"]) {
            titleStr += '  ,';
        } else {
        	var jkje = excelData[i]["zhichumingxi$金额$金额"].replace(/,/g,'');
            titleStr += jkje + '\t,';
        }
        //excelData[i]["zhichumingxi$录入人$录入人"]
        if (!excelData[i]["zhichumingxi$录入人$录入人"]) {
            titleStr += '  ,';
        } else {
        	var zclrrStr = (excelData[i]["zhichumingxi$录入人$录入人"].replace(/,/g,'.')).replace(/[\r\n\↵]/g,"");
            titleStr += zclrrStr + '\t,';
        }
        //excelData[i]["zhichumingxi$支出类别$支出类别"]
        if (!excelData[i]["zhichumingxi$支出类别$支出类别"]) {
            titleStr += '  ,';
        } else {
        	var zczclbStr = excelData[i]["zhichumingxi$支出类别$支出类别"].replace(/[\r\n\↵]/g,"");
            titleStr += zczclbStr + '\t,';
        }
        //excelData[i]["zhichumingxi$具体描述$具体描述"]
        if (!excelData[i]["zhichumingxi$具体描述$具体描述"]) {
            titleStr += '  ,';
        } else {
        	var zcjtmsStr = (excelData[i]["zhichumingxi$具体描述$具体描述"].replace(/,/g,'.')).replace(/[\r\n\↵]/g,"");
            titleStr += zcjtmsStr + '\t,';
        }
        //excelData[i]["zhichumingxi$数据来源$数据来源"]
        if (!excelData[i]["zhichumingxi$数据来源$数据来源"]) {
            titleStr += '  ,';
        } else {
        	var zcsjlyStr = excelData[i]["zhichumingxi$数据来源$数据来源"].replace(/[\r\n\↵]/g,"");
            titleStr += zcsjlyStr + '\t,';
        }
        //excelData[i]["zhichumingxi$本部合同$本部合同"]
        if (!excelData[i]["zhichumingxi$本部合同$本部合同"]) {
            titleStr += '  ,';
        } else {
        	var zcbbhtStr = excelData[i]["zhichumingxi$本部合同$本部合同"].replace(/[\r\n\↵]/g,"");
            titleStr += zcbbhtStr + '\t,';
        }
        //excelDatasArr[j][i]["zhichumingxi$业务员编码$业务员编码"]
        if(!excelData[i]["zhichumingxi$业务员编码$业务员编码"]) {
            titleStr += '  ,';
        }else{
        	var zcjywbmStr = excelData[i]["zhichumingxi$业务员编码$业务员编码"].replace(/[\r\n\↵]/g,"");
            titleStr += zcjywbmStr + '\t,';
        }
        //======
        titleStr += '\n';
    }
    return titleStr;
}
//统计报表
function tongjiExcel(titleArr,excelData){
	var titleTextArr = ["合同号","总分包","合同类型","对方单位","合同名称","合同日期","签约金额","标的价","审定价","进款","综合费率",
		"综合管理费","材料费","人工费","机械使用费","其他直接费","间接费用","报表支出","支出合计","小押金","管理费","税费","可支取余额","应收款",
		"余额","业务员","地区","开票额","竣工否","结账否","合同已到","本部合同"];				//标题列表数组
    //标题列表数组
//    for (var t = 0; t < titleArr.length; t++) {
//        titleTextArr.push(titleArr[t].column_name.split("$")[2]);
//    }
    //标题列表字符串
    var titleStr = titleTextArr.join();
    titleStr += "\n";
    for(var i = 0; i < excelData.length; i++){
        //excelData[i]["zonghechaxun$合同号$合同号"]
        if (!excelData[i]["zonghechaxun$合同号$合同号"]) {
            titleStr += '  ,';
        } else {
        	var tjhthStr = excelData[i]["zonghechaxun$合同号$合同号"].replace(/[\r\n\↵]/g,"");
            titleStr += tjhthStr + '\t,';
        }
        //excelData[i]["zonghechaxun$总分包$总分包"]
        if (!excelData[i]["zonghechaxun$总分包$总分包"]) {
            titleStr += '  ,';
        } else {
        	var tjzfbStr = excelData[i]["zonghechaxun$总分包$总分包"].replace(/[\r\n\↵]/g,"");
            titleStr += tjzfbStr + '\t,';
        }
        //excelData[i]["zonghechaxun$合同类型$合同类型"]
        if (!excelData[i]["zonghechaxun$合同类型$合同类型"]) {
            titleStr += '  ,';
        } else {
        	var tjhtlxStr = excelData[i]["zonghechaxun$合同类型$合同类型"].replace(/[\r\n\↵]/g,"");
            titleStr += tjhtlxStr + '\t,';
        }
        //excelData[i]["zonghechaxun$对方单位$对方单位"]
        if (!excelData[i]["zonghechaxun$对方单位$对方单位"]) {
            titleStr += '  ,';
        } else {
        	var tjdfdwStr = (excelData[i]["zonghechaxun$对方单位$对方单位"].replace(/,/g,'，')).replace(/[\r\n\↵]/g,"");
            titleStr += tjdfdwStr + '\t,';
        }
        //excelData[i]["zonghechaxun$合同名称$合同名称"]
        if (!excelData[i]["zonghechaxun$合同名称$合同名称"]) {
            titleStr += '  ,';
        } else {
        	var tjhtmcStr = (excelData[i]["zonghechaxun$合同名称$合同名称"].replace(/,/g,'，')).replace(/[\r\n\↵]/g,"");
            titleStr += tjhtmcStr + '\t,';
        }
        //excelData[i]["zonghechaxun$合同日期$合同日期"]
        if (!excelData[i]["zonghechaxun$合同日期$合同日期"]) {
            titleStr += '  ,';
        } else {
        	var tjhtrqStr = excelData[i]["zonghechaxun$合同日期$合同日期"].replace(/[\r\n\↵]/g,"");
            titleStr += tjhtrqStr + '\t,';
        }
        //excelData[i]["zonghechaxun$签约金额$签约金额"]
        if (!excelData[i]["zonghechaxun$签约金额$签约金额"]){
            titleStr += '  ,';
        } else {
        	var tjqyjeStr = excelData[i]["zonghechaxun$签约金额$签约金额"].replace(/[\r\n\↵]/g,"").replace(/\,/g,"");
            titleStr += tjqyjeStr + '\t,';
        }
        //excelData[i]["zonghechaxun$标的价$标的价"]
        if (!excelData[i]["zonghechaxun$标的价$标的价"]) {
            titleStr += '  ,';
        } else {
        	var tjbdjStr = excelData[i]["zonghechaxun$标的价$标的价"].replace(/[\r\n\↵]/g,"").replace(/\,/g,"");
            titleStr += tjbdjStr + '\t,';
        }
        //excelData[i]["zonghechaxun$审定价$审定价"]
        if (!excelData[i]["zonghechaxun$审定价$审定价"]) {
            titleStr += '  ,';
        } else {
        	var tjsdjStr = excelData[i]["zonghechaxun$审定价$审定价"].replace(/[\r\n\↵]/g,"").replace(/\,/g,"");
            titleStr += tjsdjStr + '\t,';
        }
        //excelData[i]["zonghechaxun$进款$进款"]
        if (!excelData[i]["zonghechaxun$进款$进款"]) {
            titleStr += '  ,';
        } else {
        	var tjjkStr = excelData[i]["zonghechaxun$进款$进款"].replace(/[\r\n\↵]/g,"").replace(/\,/g,"");
            titleStr += tjjkStr + '\t,';
        }
        //excelData[i]["zonghechaxun$综合费率$综合费率"]
        if (!excelData[i]["zonghechaxun$综合费率$综合费率"]) {
            titleStr += '  ,';
        } else {
    
        	var tjzhflStr = (excelData[i]["zonghechaxun$综合费率$综合费率"] + "").replace(/[\r\n\↵]/g,"").replace(/\,/g,"");
            titleStr += tjzhflStr + '\t,';
        }
        //excelData[i]["zonghechaxun$综合管理费$综合管理费"]
        if (!excelData[i]["zonghechaxun$综合管理费$综合管理费"]) {
            titleStr += '  ,';
        } else {
        	var tjzhglfStr = excelData[i]["zonghechaxun$综合管理费$综合管理费"].replace(/[\r\n\↵]/g,"").replace(/\,/g,"");
            titleStr += tjzhglfStr + '\t,';
        }
        //excelData[i]["zonghechaxun$材料费$材料费"]
        if (!excelData[i]["zonghechaxun$材料费$材料费"]) {
            titleStr += '  ,';
        } else {
        	var tjclfStr = excelData[i]["zonghechaxun$材料费$材料费"].replace(/[\r\n\↵]/g,"").replace(/\,/g,"");
            titleStr += tjclfStr + '\t,';
        }
        //excelData[i]["zonghechaxun$人工费$人工费"]
        if (!excelData[i]["zonghechaxun$人工费$人工费"]) {
            titleStr += '  ,';
        } else {
        	var tjrgfStr = excelData[i]["zonghechaxun$人工费$人工费"].replace(/[\r\n\↵]/g,"").replace(/\,/g,"");
            titleStr += tjrgfStr + '\t,';
        }
        //excelData[i]["zonghechaxun$机械使用费$机械使用费"]
        if (!excelData[i]["zonghechaxun$机械使用费$机械使用费"]) {
            titleStr += '  ,';
        } else {
        	var tjjxsyfStr = excelData[i]["zonghechaxun$机械使用费$机械使用费"].replace(/[\r\n\↵]/g,"").replace(/\,/g,"");
            titleStr += tjjxsyfStr + '\t,';
        }
        //excelData[i]["zonghechaxun$其他直接费$其他直接费"]
        if (!excelData[i]["zonghechaxun$其他直接费$其他直接费"]) {
            titleStr += '  ,';
        } else {
        	var tjqtzjffStr = excelData[i]["zonghechaxun$其他直接费$其他直接费"].replace(/[\r\n\↵]/g,"").replace(/\,/g,"");
            titleStr += tjqtzjffStr + '\t,';
        }
        //excelData[i]["zonghechaxun$间接费用$间接费用"]
        if (!excelData[i]["zonghechaxun$间接费用$间接费用"]) {
            titleStr += '  ,';
        } else {
        	var tjjjfyStr = excelData[i]["zonghechaxun$间接费用$间接费用"].replace(/[\r\n\↵]/g,"").replace(/\,/g,"");
            titleStr += tjjjfyStr + '\t,';
        }
        //excelData[i]["zonghechaxun$报表支出$报表支出"]
        if (!excelData[i]["zonghechaxun$报表支出$报表支出"]) {
            titleStr += '  ,';
        } else {
        	var tjbbzcStr = excelData[i]["zonghechaxun$报表支出$报表支出"].replace(/[\r\n\↵]/g,"").replace(/\,/g,"");
            titleStr += tjbbzcStr + '\t,';
        }
        //excelData[i]["zonghechaxun$支出合计$支出合计"]
        if (!excelData[i]["zonghechaxun$支出合计$支出合计"]) {
            titleStr += '  ,';
        } else {
        	var tjzchjStr = excelData[i]["zonghechaxun$支出合计$支出合计"].replace(/[\r\n\↵]/g,"").replace(/\,/g,"");
            titleStr += tjzchjStr + '\t,';
        }
        //excelData[i]["zonghechaxun$小押金$小押金"]
        if (!excelData[i]["zonghechaxun$小押金$小押金"]) {
            titleStr += '  ,';
        } else {
        	var tjxyjStr = excelData[i]["zonghechaxun$小押金$小押金"].replace(/[\r\n\↵]/g,"").replace(/\,/g,"");
            titleStr += tjxyjStr + '\t,';
        }
        //excelData[i]["zonghechaxun$管理费$管理费"]
        if (!excelData[i]["zonghechaxun$管理费$管理费"]) {
            titleStr += '  ,';
        } else {
        	var tjglfStr = (excelData[i]["zonghechaxun$管理费$管理费"]+"").replace(/[\r\n\↵]/g,"").replace(/\,/g,"");
            titleStr += tjglfStr + '\t,';
        }
        //excelData[i]["zonghechaxun$税费$税费"]
        if (!excelData[i]["zonghechaxun$税费$税费"]) {
        	console.log(excelData[i]["zonghechaxun$税费$税费"],"空");
            titleStr += '  ,';
        } else {
        	var tjsfStr = (excelData[i]["zonghechaxun$税费$税费"]+"").replace(/[\r\n\↵]/g,"").replace(/\,/g,"");
            titleStr += tjsfStr + '\t,';
        }
        //excelData[i]["zonghechaxun$可支取余额$可支取余额"]
        if (!excelData[i]["zonghechaxun$可支取余额$可支取余额"]) {
            titleStr += '  ,';
        } else {
        	var tjkzqyetr = (excelData[i]["zonghechaxun$可支取余额$可支取余额"]+"").replace(/[\r\n\↵]/g,"").replace(/\,/g,"");
        	titleStr += tjkzqyetr + '\t,';
        }
        //excelData[i]["zonghechaxun$应收款$应收款"]
        if (!excelData[i]["zonghechaxun$应收款$应收款"]) {
            titleStr += '  ,';
        } else {
        	var tjyeStr = excelData[i]["zonghechaxun$应收款$应收款"].replace(/[\r\n\↵]/g,"").replace(/\,/g,"");
            titleStr += tjyeStr + '\t,';
        }
        //excelData[i]["zonghechaxun$余额$余额"]
        if (!excelData[i]["zonghechaxun$余额$余额"]) {
            titleStr += '  ,';
        } else {
        	var tjyeStr = excelData[i]["zonghechaxun$余额$余额"].replace(/[\r\n\↵]/g,"").replace(/\,/g,"");
            titleStr += tjyeStr + '\t,';
        }
//        //excelData[i]["zonghechaxun$合同应收款$合同应收款"]
//        if (!excelData[i]["zonghechaxun$合同应收款$合同应收款"]) {
//            titleStr += ' 空,';
//        } else {
//        	var tjhtyskStr = excelData[i]["zonghechaxun$合同应收款$合同应收款"].replace(/[\r\n\↵]/g,"").replace(/\,/g,"，");
//            titleStr += tjhtyskStr + '\t,';
//        }
        //excelData[i]["zonghechaxun$业务员$业务员"]
        if (!excelData[i]["zonghechaxun$业务员$业务员"]) {
            titleStr += '  ,';
        } else {
        	var tjywyStr = excelData[i]["zonghechaxun$业务员$业务员"].replace(/[\r\n\↵]/g,"").replace(/\,/g,"");
            titleStr += tjywyStr + '\t,';
        }
        //excelData[i]["zonghechaxun$地区$地区"]
        if (!excelData[i]["zonghechaxun$地区$地区"]) {
            titleStr += '  ,';
        } else {
        	var tjdqStr = excelData[i]["zonghechaxun$地区$地区"].replace(/[\r\n\↵]/g,"").replace(/\,/g,"");
            titleStr += tjdqStr + '\t,';
        }
        //excelData[i]["zonghechaxun$开票额$开票额"]
        if (!excelData[i]["zonghechaxun$开票额$开票额"]) {
            titleStr += '  ,';
        } else {
        	var tjkpeStr = excelData[i]["zonghechaxun$开票额$开票额"].replace(/[\r\n\↵]/g,"").replace(/\,/g,"");
            titleStr += tjkpeStr + '\t,';
        }
        //excelData[i]["zonghechaxun$竣工否$竣工否"]
        if (!excelData[i]["zonghechaxun$竣工否$竣工否"]) {
            titleStr += '  ,';
        } else {
        	var tjjgfStr = excelData[i]["zonghechaxun$竣工否$竣工否"].replace(/[\r\n\↵]/g,"");
            titleStr += tjjgfStr + '\t,';
        }
        //excelData[i]["zonghechaxun$结账否$结账否"]
        if (!excelData[i]["zonghechaxun$结账否$结账否"]) {
            titleStr += '  ,';
        } else {
        	var tjjzfStr = excelData[i]["zonghechaxun$结账否$结账否"].replace(/[\r\n\↵]/g,"");
            titleStr += tjjzfStr + '\t,';
        }
        //excelData[i]["zonghechaxun$合同已到$合同已到"]
        if (!excelData[i]["zonghechaxun$合同已到$合同已到"]) {
            titleStr += '  ,';
        } else {
        	var tjhtydStr = excelData[i]["zonghechaxun$合同已到$合同已到"].replace(/[\r\n\↵]/g,"");
            titleStr += tjhtydStr + '\t,';
        }
        //excelData[i]["zonghechaxun$本部合同$本部合同"]
        if (!excelData[i]["zonghechaxun$本部合同$本部合同"]) {
            titleStr += '  ,';
        } else {
        	var tjbbhtStr = excelData[i]["zonghechaxun$本部合同$本部合同"].replace(/[\r\n\↵]/g,"");
            titleStr += tjbbhtStr + '\t,';
        }
        //======
        titleStr += '\n';
    }
    return titleStr;
}
//分组函数（将每n个值为一组导出）
function SplitArray (N,Q){
    var R = [],F;
    for (F = 0;F < Q.length;) R.push(Q.slice(F,F += N))
    return R
}
//可折叠搜索条件框
function toggleFieldSet(ck, id) {
    var dom = document.getElementById(id);
    dom.className = !ck.checked ? "hideFieldset" : "";
}
//排序
function sortApply(){
	var sortFiledVal = sortFiledInput.getValue();
	var sortOrder = ascDesc.getValue();
	//输入列名（不输入排序方向，默认正序）
	if(sortFiledVal){
		var searchSql = searchSQL();
		var obj = {
				reportId : childReportId,
	            paramSql : searchSql,
	            sortFiledVal : sortFiledVal,
	            sortOrder : sortOrder
	    }
		var jsonString = JSON.stringify(obj);
		grid.loading("加载中，请稍后......");
		$.ajax({
			url: ajaxUrl + "/reportController/list.do",
			type:"post",
			contentType:"application/json",
			data : jsonString,
			success : function(data){
				console.log(data);
				grid.setData(data.data);
				countGrid.setData(data.countDate);
			},
			error : function(msg){
				console.log(msg);
			}
		});
	}else{
		mini.alert("请选择列名");
	}
}
//拼写sql语句
function searchSQL(){
	//搜索框数据
	var formData = pubSerchData;
	//存放sql
    var dateStr = "";  
    //存放sql的数组
    var dateArr = []; 
    //遍历搜索框
    for( var i = 0; i < formData.length; i++){
        var inputBoxs = mini.getByName(formData[i].dataSource + "1");
        //判断有没有2（区间）
        if(formData[i].type == "3" || formData[i].type == "4"){
        	var inputBoxs2 = mini.getByName(formData[i].dataSource + "2");
        	 //值不为空
            if(inputBoxs.getValue() || inputBoxs2.getValue()){
                //如果是文本
                if(formData[i].type == "1" || formData[i].type == "5"){
                    var colums = formData[i].dataSource;
                    var colums1 = mini.getByName(formData[i].dataSource + "1").getValue();
                    if(colums1 != ""){
                        dateStr = " and "  + colums + " like '%" + colums1 + "%'";
                        dateArr.push(dateStr);
                    }else{
                        /*dateStr = " and "  + colums + " = " + colums;
                        dateArr.push(dateStr);*/
                    }
                //如果是日期
                }else if(formData[i].type == "2"){
                    var colums = formData[i].dataSource;
                    var colums1 = mini.getByName(formData[i].dataSource + "1").getValue();
                    if(colums1 != ""){
                        colums1 = mini.formatDate(colums1,"yyyy-MM-dd");       //时间格式
                        dateStr = " and CONVERT(varchar(100)," + colums +",23)='" + colums1 + "'";
                        dateArr.push(dateStr);
                    }else{
                        /*dateStr = " and "  + colums + " = " + colums;
                        dateArr.push(dateStr);*/
                    }
                //如果是文本区间
                }else if(formData[i].type == "3"){
                    var colums= formData[i].dataSource;
                    var colum1 = mini.getByName(formData[i].dataSource + "1").getValue();
                    var colum2 = mini.getByName(formData[i].dataSource + "2").getValue();
                    //判断字符串中是否含有逗号
                    var regDouhao = /,/;
                    if(regDouhao.test(colum1) || regDouhao.test(colum2)){
                    	//将带有逗号的金额数字去掉逗号，转为数字
                        colum1 = colum1.replace(/,/g,'');
                        colum2 = colum2.replace(/,/g,'');
                    }
                    if(colum1 == "" && colum2 != ""){
                        dateStr = " and "  + colums + " < " + "'" + colum2 + "'";
                        dateArr.push(dateStr);
                    }else if(colum1 != "" && colum2 == ""){
                    	dateStr = " and "  + colums + " > " + "'" + colum1 + "'";
                        dateArr.push(dateStr);
                    }else if(colum1 != "" && colum2 != ""){
                        if(colum1 < colum2){
                            dateStr = " and " + colums + " between '" + colum1 + "'" + " and '" + colum2 + "'";
                            dateArr.push(dateStr);
                        }else{
                            mini.alert("第一个数字不可以大于第二个数字");
                        }
                    }
                //如果是日期区间
                }else if(formData[i].type == "4"){
                    var colums= formData[i].dataSource;
                    //通过name得到日期区间得元素并且得到用户填写得值
                    var colum1 = mini.getByName(formData[i].dataSource + "1").getValue();
                    var colum2 = mini.getByName(formData[i].dataSource + "2").getValue();
                    //如果值都不为空，那么执行sql语句
                    if(colums1 != "" && colum2 != ""){
                        if(tab(colum1,colum2)){
                            colum1 = mini.formatDate(colum1,"yyyy-MM-dd");       //时间格式
                            colum2 = mini.formatDate(colum2,"yyyy-MM-dd");       //时间格式
                            colum1 = colum1 + " 00:00:00";
                            colum2 = colum2 + " 23:59:59";
                            dateStr = " and " + colums + " between '" + colum1 + "' and " + "'" + colum2 + "'";
                            dateArr.push(dateStr);
                        }else{
                            mini.alert("请输入正确的日期区间");
                        }
                    }else if(colums1 == "" && colum2 != ""){
                        colum2 = mini.formatDate(colum2,"yyyy-MM-dd");       //时间格式
                        dateStr = " and "  + colums + " <= " + "'" + colum2 + "'";
                        dateArr.push(dateStr);
                    }else if(colums1 != "" && colum2 == ""){
                        colum1 = mini.formatDate(colum1,"yyyy-MM-dd");       //时间格式
                    	dateStr = " and "  + colums + " >= " + "'" + colum1 + "'";
                        dateArr.push(dateStr);
                    }
                }else if(formData[i].type == "5"){
                    var colums = formData[i].dataSource;
                    var colums1 = mini.getByName(formData[i].dataSource + "1").getValue();
                    if(colums1 != ""){
                        dateStr = " and "  + colums + " like '%" + colums1 + "%'";
                        dateArr.push(dateStr);
                    }else{
                        /*dateStr = " and "  + colums + " = " + colums;
                         dateArr.push(dateStr);*/
                    }
                    //如果是日期
                }
            }else{

            }
        }else{
        	 //值不为空
            if(inputBoxs.getValue()){
                //如果是文本
                if(formData[i].type == "1" || formData[i].type == "5"){
                    var colums = formData[i].dataSource;
                    var colums1 = mini.getByName(formData[i].dataSource + "1").getValue();
                    if(colums1 != ""){
                        dateStr = " and "  + colums + " like '%" + colums1 + "%'";
                        dateArr.push(dateStr);
                    }else{
                        /*dateStr = " and "  + colums + " = " + colums;
                        dateArr.push(dateStr);*/
                    }
                //如果是日期
                }else if(formData[i].type == "2"){
                    var colums = formData[i].dataSource;
                    var colums1 = mini.getByName(formData[i].dataSource + "1").getValue();
                    if(colums1 != ""){
                        colums1 = mini.formatDate(colums1,"yyyy-MM-dd");       //时间格式
                        dateStr = " and CONVERT(varchar(100)," + colums +",23)='" + colums1 + "'";
                        dateArr.push(dateStr);
                    }else{
                        /*dateStr = " and "  + colums + " = " + colums;
                        dateArr.push(dateStr);*/
                    }
                //如果是文本区间
                }else if(formData[i].type == "3"){
                    var colums= formData[i].dataSource;
                    var colum1 = mini.getByName(formData[i].dataSource + "1").getValue();
                    var colum2 = mini.getByName(formData[i].dataSource + "2").getValue();
                    //判断字符串中是否含有逗号
                    var regDouhao = /,/;
                    if(regDouhao.test(colum1) || regDouhao.test(colum2)){
                    	//将带有逗号的金额数字去掉逗号，转为数字
                        colum1 = colum1.replace(/,/g,'');
                        colum2 = colum2.replace(/,/g,'');
                    }
                    if(colum1 == "" && colum2 != ""){
                        dateStr = " and "  + colums + " < " + "'" + colum2 + "'";
                        dateArr.push(dateStr);
                    }else if(colum1 != "" && colum2 == ""){
                    	dateStr = " and "  + colums + " > " + "'" + colum1 + "'";
                        dateArr.push(dateStr);
                    }else if(colum1 != "" && colum2 != ""){
                        if(colum1 < colum2){
                            dateStr = " and " + colums + " between '" + colum1 + "'" + " and '" + colum2 + "'";
                            dateArr.push(dateStr);
                        }else{
                            mini.alert("第一个数字不可以大于第二个数字");
                        }
                    }
                //如果是日期区间
                }else if(formData[i].type == "4"){
                    var colums= formData[i].dataSource;
                    //通过name得到日期区间得元素并且得到用户填写得值
                    var colum1 = mini.getByName(formData[i].dataSource + "1").getValue();
                    var colum2 = mini.getByName(formData[i].dataSource + "2").getValue();
                    //如果值都不为空，那么执行sql语句
                    if(colums1 != "" && colum2 != ""){
                        if(tab(colum1,colum2)){
                            colum1 = mini.formatDate(colum1,"yyyy-MM-dd");       //时间格式
                            colum2 = mini.formatDate(colum2,"yyyy-MM-dd");       //时间格式
                            colum1 = colum1 + " 00:00:00";
                            colum2 = colum2 + " 23:59:59";
                            dateStr = " and " + colums + " between '" + colum1 + "' and " + "'" + colum2 + "'";
                            dateArr.push(dateStr);
                        }else{
                            mini.alert("请输入正确的日期区间");
                        }
                    }else if(colums1 == "" && colum2 != ""){
                        colum2 = mini.formatDate(colum2,"yyyy-MM-dd");       //时间格式
                        dateStr = " and "  + colums + " <= " + "'" + colum2 + "'";
                        dateArr.push(dateStr);
                    }else if(colums1 != "" && colum2 == ""){
                        colum1 = mini.formatDate(colum1,"yyyy-MM-dd");       //时间格式
                    	dateStr = " and "  + colums + " >= " + "'" + colum1 + "'";
                        dateArr.push(dateStr);
                    }
                }else if(formData[i].type == "5"){
                    var colums = formData[i].dataSource;
                    var colums1 = mini.getByName(formData[i].dataSource + "1").getValue();
                    if(colums1 != ""){
                        dateStr = " and "  + colums + " like '%" + colums1 + "%'";
                        dateArr.push(dateStr);
                    }else{
                        /*dateStr = " and "  + colums + " = " + colums;
                         dateArr.push(dateStr);*/
                    }
                    //如果是日期
                }
            }else{

            }
        }
       
    }
    var searchSql = dateArr.join("");
    console.log(searchSql,"searchSql")
    return searchSql;
}
//========================================================================================
//这里的数据是用来定义导出的格式类型
const wopts = { bookType: 'xlsx', bookSST: false, type: 'binary' };
//导出Excel（插件）
function downloadExl() {
	//请求数据
	var searchSql = searchSQL();
	var nowPageIdx = grid.getPageIndex();  //当前页码
	var searchSqlData = {
			reportId: childReportId,
	        paramSql: searchSql,
	        sortFiledVal : "",
	        sortOrder : ""
	}
	var jsonString = JSON.stringify(searchSqlData);
	grid.loading("加载中，请稍后......");
	$.ajax({
		url: ajaxUrl + "/reportController/excelExport.do",
		type: "POST",
		data: jsonString,
		contentType: "application/json",
		success: function (data) {
			//取消遮照
			grid.unmask();
			console.log(data,"导出Excel数据");
			excelData = data.data; 
			var rowsName = {};
			if(pubReportName == "合同查询"){
				rowsName = {
						"HeTongView$合同号$合同号" : "合同号",
						"HeTongView$合同类型$合同类型" : "合同类型",
						"HeTongView$合同名称$合同名称" : "合同名称",
						"HeTongView$对方单位$对方单位" : "对方单位",
						"HeTongView$签约金额$签约金额" : "签约金额",
						"HeTongView$合同日期$合同日期": "合同日期",
						"HeTongView$综合费率$综合费率" : "综合费率",
						"HeTongView$发票税率$发票税率" : "发票税率",
						"HeTongView$管理费率$管理费率" : "管理费率",
						"HeTongView$录入人$录入人" : "录入人",
						"HeTongView$收付类型$收付类型" : "收付类型",
						"HeTongView$审定价$审定价" : "审定价",
						"HeTongView$地区$地区" : "地区",
						"HeTongView$标的价$标的价" : "标的价",
						"HeTongView$部门$部门" : "部门",
						"HeTongView$业务员$业务员"  : "业务员",
						"HeTongView$竣工否$竣工否" : "竣工否",
						"HeTongView$结账否$结账否"  : "结账否",
						"HeTongView$备注$备注" : "备注",
						"HeTongView$总分包$总分包" : "总分包",
						"HeTongView$合同已到$合同已到" : "合同已到",
						"HeTongView$本部合同$本部合同" : "本部合同",
						"HeTongView$合同收取日期$合同收取日期" : "合同收取日期",
						"HeTongView$竣工报告日期$竣工报告日期" : "竣工报告日期"
						
				}
			}else if(pubReportName == "进款明细"){
				rowsName = {
						"jinkuanmingxi$合同号$合同号" : "合同号",
						"jinkuanmingxi$业务员$业务员" : "业务员",
						"jinkuanmingxi$部门$部门" : "部门",
						"jinkuanmingxi$合同类型$合同类型" : "合同类型",
						"jinkuanmingxi$合同名称$合同名称" : "合同名称",
						"jinkuanmingxi$单据日期$单据日期" : "单据日期",
						"jinkuanmingxi$单据号$单据号" : "单据号",
						"jinkuanmingxi$凭证号$凭证号" : "凭证号",
						"jinkuanmingxi$金额$金额" : "金额",
						"jinkuanmingxi$本部合同$本部合同" : "本部合同",
						"jinkuanmingxi$竣工否$竣工否" : "竣工否",
						"jinkuanmingxi$管理费$管理费" : "管理费",
						"jinkuanmingxi$对方单位$对方单位" : "对方单位"
				}
			}else if(pubReportName == "开票查询"){
				rowsName = {
						"kaipiaomingxi$录入时间$录入时间" : "录入时间",
						"kaipiaomingxi$结算单号$结算单号" : "结算单号",
						"kaipiaomingxi$合同号$合同号" : "合同号",
						"kaipiaomingxi$凭证号$凭证号" : "凭证号",
						"kaipiaomingxi$金额$金额" : "金额",
						"kaipiaomingxi$开票地域$开票地域" : "开票地域",
						"kaipiaomingxi$发票类型$发票类型" : "发票类型",
						"kaipiaomingxi$发票费率$发票费率" : "发票费率",
						"kaipiaomingxi$综合费率$综合费率" : "综合费率",
						"kaipiaomingxi$综合税费$综合税费" : "综合税费",
						"kaipiaomingxi$业务员$业务员" : "业务员"
				}
			}else if(pubReportName == "支出明细"){
				rowsName = {
						"zhichumingxi$合同号$合同号" : "合同号",
						"zhichumingxi$业务员$业务员" : "业务员",
						"zhichumingxi$合同名称$合同名称" : "合同名称",
						"zhichumingxi$供应商$供应商" : "供应商",
						"zhichumingxi$合同类型$合同类型" : "合同类型",
						"zhichumingxi$单据日期$单据日期" : "单据日期",
						"zhichumingxi$单据号$单据号" : "单据号",
						"zhichumingxi$凭证号$凭证号" : "凭证号",
						"zhichumingxi$金额$金额" : "金额",
						"zhichumingxi$录入人$录入人" : "录入人",
						"zhichumingxi$支出类别$支出类别" : "支出类别",
						"zhichumingxi$具体描述$具体描述" : "具体描述",
						"zhichumingxi$数据来源$数据来源" : "数据来源",
						"zhichumingxi$本部合同$本部合同" : "本部合同",
						"zhichumingxi$业务员编码$业务员编码" : "业务员编码",
				}
			}else if(pubReportName == "统计报表"){
				rowsName = {
						"zonghechaxun$合同号$合同号" : "合同号",
						"zonghechaxun$业务员$业务员" : "业务员",
						"zonghechaxun$合同名称$合同名称" : "合同名称",
						"zonghechaxun$签约金额$签约金额" : "签约金额",
						"zonghechaxun$审定价$审定价" : "审定价",
						"zonghechaxun$进款$进款" : "进款",
						"zonghechaxun$支出合计$支出合计" : "支出合计",
						"zonghechaxun$余额$余额" : "余额",
						"zonghechaxun$可支取余额$可支取余额" : "可支取余额",
						"zonghechaxun$间接费用$间接费用" : "间接费用",
						"zonghechaxun$对方单位$对方单位" : "对方单位",
						"zonghechaxun$开票额$开票额" : "开票额",
						"zonghechaxun$小押金$小押金" : "小押金",
						"zonghechaxun$管理费$管理费" : "管理费",
						"zonghechaxun$税费$税费" : "税费",
						"zonghechaxun$综合费率$综合费率" : "综合费率",
						"zonghechaxun$综合管理费$综合管理费" : "综合管理费",
						"zonghechaxun$材料费$材料费" : "材料费",
						"zonghechaxun$人工费$人工费" : "人工费",
						"zonghechaxun$机械使用费$机械使用费" : "机械使用费",
						"zonghechaxun$其他直接费$其他直接费" : "其他直接费",
						"zonghechaxun$报表支出$报表支出" : "报表支出",
						"zonghechaxun$应收款$应收款" : "应收款",
						"zonghechaxun$地区$地区" : "地区",
						"zonghechaxun$总分包$总分包" : "总分包",
						"zonghechaxun$合同类型$合同类型" : "合同类型",
						"zonghechaxun$合同日期$合同日期" : "合同日期",
						"zonghechaxun$标的价$标的价" : "标的价",
						"zonghechaxun$本部合同$本部合同" : "本部合同",
						"zonghechaxun$合同已到$合同已到" : "合同已到",
						"zonghechaxun$结账否$结账否" : "结账否",
						"zonghechaxun$竣工否$竣工否" : "竣工否",
				}
			}
			//excelData.unshift(rowsName);
			//处理数据：将列名（json的key转化为中文）
			excelData.forEach(function(item){
	            for(var k in item){
	                var ks = k.split("$")[2];
	                item[ks] = item[k];
	                delete item[k];
	            }
	        });
			//创建规范的workbook（Excel文档）
			const wb = { 
					SheetNames: ['Sheet1'], 	//工作表名数组
					Sheets: {}, 				//工作表对象 键名对应SheetNames的key				
					Props: {} 
			};
		    wb.Sheets['Sheet1'] = XLSX.utils.json_to_sheet(excelData);//通过json_to_sheet转成单页(Sheet)数据
		    console.log(wb.Sheets.Sheet1,"转成sheet的数据表");
		    saveAs(new Blob([s2ab(XLSX.write(wb, wopts))], { type: "application/octet-stream" }), data.reportParam.reportName + '.' + (wopts.bookType=="biff2"?"xls":wopts.bookType));
		    //saveAs(new Blob([s2ab(XLSX.write(wb,ws,wopts))], { type: "application/octet-stream" }), data.reportParam.reportName + '.' + (wopts.bookType=="biff2"?"xls":wopts.bookType));
		},
		error: function (data) {
			console.log(data);
		}
	});
}
function saveAs(obj, fileName) {//当然可以自定义简单的下载文件实现方式
    var tmpa = document.createElement("a");
    tmpa.download = fileName || "下载";
    tmpa.href = URL.createObjectURL(obj); //绑定a标签
    tmpa.click(); //模拟点击实现下载
    setTimeout(function () { //延时释放
        URL.revokeObjectURL(obj); //用URL.revokeObjectURL()来释放这个object URL
    }, 100);
}
function s2ab(s) {
    if (typeof ArrayBuffer !== 'undefined') {
        var buf = new ArrayBuffer(s.length);
        var view = new Uint8Array(buf);
        for (var i = 0; i != s.length; ++i) view[i] = s.charCodeAt(i) & 0xFF;
        return buf;
    } else {
        var buf = new Array(s.length);
        for (var i = 0; i != s.length; ++i) buf[i] = s.charCodeAt(i) & 0xFF;
        return buf;
    }
}
function newExcelImport(){
	//请求数据
	var searchSql = searchSQL();
	var nowPageIdx = grid.getPageIndex();  //当前页码
	var searchSqlData = {
			reportId: childReportId,
	        paramSql: searchSql,
	        sortFiledVal : "",
	        sortOrder : ""
	}
	var jsonString = JSON.stringify(searchSqlData);
	grid.loading("加载中，请稍后......");
	$.ajax({
		url: ajaxUrl + "/reportController/excelExport.do",
		type: "POST",
		data: jsonString,
		contentType: "application/json",
		success : function (data){
			//取消遮照
			grid.unmask();
			console.log(data,"导出Excel数据");
			var columnCnTypeArr = data.columnCnType;
			var titleArr123 = [];
			for(var i = 0; i < columnCnTypeArr.length; i++){
				titleArr123.push(columnCnTypeArr[i].column_name);
			}
			excelData = data.data; 
			console.log(titleArr123,"标题数组");
			//自定义标题栏
		    var title=["业务员","发票税率","合同号","合同名称","合同已到","合同收取日期","合同日期","合同类型",
		        "地区","备注","审定价","对方单位","录入人","总分包","收付类型","本部合同","标的价","竣工否",
		        "竣工报告日期","签约金额","管理费率","结账否","综合费率","部门"
		    ];
		    //处理数据：将k补充完整
		    excelData.forEach(function(item){
		    	titleArr123.forEach(function(v){
	                if(item[v] == undefined){
	                    item[v] = "";
	                }
	            })
	        });
		    console.log(excelData,"格式化后的数据");
		    //自定义过滤栏（不需要导出的行）
		    //var filter=['id','logins'];
		    //自定义导出1
		    //JSONToExcelConvertor(excelData,"report",title,filter);
		    //自定义导出1
		    JSONToExcelConvertor(excelData,"report",titleArr123);
		}
	});
}
